package loengud.w14;

/**
 * Example of employee's supervisors (linked objects).
 * @author Ago
 *
 */
public class EmployeeExample {

	/**
	 * Holds data about one employee.
	 *
	 */
	class Employee {
		/**
		 * Name of the employee.
		 */
		String name;
		/**
		 * The employee's supervisor.
		 */
		Employee supervisor;
		
		public Employee(String name) {
			this.name = name;
		}
		
		public Employee(String name, Employee supervisor) {
			this.name = name;
			this.supervisor = supervisor;
		}
	}
	
	public static void main(String[] args) {
		new EmployeeExample();
	}
	
	public EmployeeExample() {
		Employee e1 = new Employee("John");
		Employee e2 = new Employee("Chris", e1);
		Employee e3 = new Employee("Pat", e2);
		Employee e4 = new Employee("Ann", e2);
		Employee e5 = new Employee("David", e4);
		
		printStepsToBoss(e1);
		printStepsToBoss(e2);
		printStepsToBoss(e3);
		printStepsToBoss(e4);
		printStepsToBoss(e5);
	}

	public void printStepsToBoss(Employee emp) {
		if (emp.supervisor == null) {
			System.out.println(emp.name + " is the boss!");
		} else {
			// for "running" up the chain of command
			Employee runner;
			runner = emp.supervisor;
			
			if (runner.supervisor == null) {
				System.out.println(emp.name + " reports directly to the boss.");
			} else {
				int count = 0;
				// "climb" up the chain, until we reach to the top (boss)
				while (runner.supervisor != null) {
					count++; // count the steps
					runner = runner.supervisor;
				}
				System.out.println("There are " + count
						+ " supervisors between " + emp.name
						+ " and the boss");
			}
		}
	}
}
